var gulp = require("gulp");
var webserver = require("gulp-webserver");
var livereload = require("gulp-livereload");
var sass = require("gulp-ruby-sass");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var sourcemap = require("gulp-sourcemaps");
var imagemin = require("gulp-imagemin");
// 自动刷新页面
gulp.task("cmk",function(){
	gulp.src("dest")
	.pipe(webserver({
		livereload:true,
		open:true
	}))
})
// 移动到开发环境
gulp.task("html",function(){
	return gulp.src("src/**/*.html")
	.pipe(gulp.dest("dest"))
})

// 监听sass
gulp.task("sass",function(){
	return sass("src/sass/*.scss",{style:"compressed"})
	.on("error",function(err){
		console.log(err)
	})
	.pipe(gulp.dest("dest/css"))
})
// 压缩JS&命名
gulp.task("js",function(){
	return gulp.src("src/js/*.js")
	.pipe(sourcemap.init())
	.pipe(rename({suffix:".min"}))
	.pipe(uglify())
	.pipe(sourcemap.write("maps"))
	.pipe(gulp.dest("dest/js"))
})
// 压缩图片
gulp.task("images",function(){
	gulp.src("src/img/*.*")
	.pipe(imagemin({
		progressive:true
	}))
	.pipe(gulp.dest("dest/img"))
})
// 监听
gulp.task("listen",function(){
	gulp.watch("src/**/*.html",["html"]);
	gulp.watch("src/sass/*.scss",["sass"]);
	gulp.watch("src/js/*.js",["js"]);
	gulp.watch("src/img/*.*",["images"])
})

gulp.task("default",["cmk","html","sass","js","images","listen"]);